package leetcode;

public class _0235 {
    /**
     * 二叉搜索树的最近公共祖先
     */
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
    class Solution {
        public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
            int x = root.val;
            if(p.val<x&&q.val<x){
                return lowestCommonAncestor(root.left,p,q);
            }
            if(p.val>x&&q.val>x){
                return lowestCommonAncestor(root.right,p,q);
            }
            return root;
        }
    }
}
